<?php
namespace app\common\model;

use think\Model;
use think\facade\Log;
use think\Db;

class Welcome extends Model
{
    /**
     * 返回layui的table所需要的格式
     * @author tony
     * @param $post
     * @return mixed
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }
        $tableWhere = $this->tableWhere($post);
 
        if(isset($tableWhere['whereor'])){
            $list = $this->field($tableWhere['field'])->whereOr($tableWhere['whereor'])->order($tableWhere['order'])->paginate($limit);
        } else {
            $list = $this->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
        }
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;

        return $re;
    }

    /**
     * 根据输入的查询条件，返回所需要的where
     * @author tony
     * @param $post
     * @return mixed
     */
    protected function tableWhere($post)
    {
        $result['where'] = [];
        $result['field'] = "*";
        $result['order'] = [];
        return $result;
    }

    /**
     * 根据查询结果，格式化数据
     * @author tony
     * @param $list
     * @return mixed
     */
    protected function tableFormat($list)
    {
        return $list;
    }

    // 获取数量：无辅导员信息
    public function get_wfdyxy_count()
    {
        $sql = "SELECT count(*) as count FROM zlcw.zl_student_info WHERE teacherId = ''";
        $res = Db::query($sql);
        return $res[0]["count"];
    }

    // 获取数量：班级变更待同步
    public function get_bjbgdtb_count()
    {
        $sql = "SELECT count(*) AS count
                FROM zlzhsf.pxsdm a
                JOIN zlzhsf.sf_ysk b ON a.xh = b.xh
                AND b.sfqjdm = (SELECT val FROM zlzhsf.pxtcs WHERE csmc = 'sfqj' )
                AND b.SFXMDM = '04' AND b.ISHZ = 1
                JOIN zl_student_info f ON a.xh = f.xh
                WHERE a.bjdm <> f.bjdm";
        $res = Db::query($sql);
        return $res[0]["count"];
    }

    // 获取数量：待关联计划
    public function get_dgljh_count()
    {
        // 调用存储过程
        $model = new Common();
        $list = $model->query("call getPlanUnrelList(0,100000);");
        
        return count($list[1]);
    }

    // 获取数量：待关联财务班级
    public function get_dglcwbj_count()
    {
        // 调用存储过程
        $model = new Common();
        $list = $model->query("call getStudentUnrelList('',0,100000);");

        return count($list[1]);
    }
}